
#include "rpucfg.h"

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <algorithm>


#if 0
int RPUConfig::createCL0File(const String &fileName){

	std::ofstream CL0File(fileName.c_str());

	assert(CL0File);

	const String & graphName = dfgGraph.name();

	// uppercase of name
	String GRAPHNAME = graphName;
	std::transform(GRAPHNAME.begin(), GRAPHNAME.end(), 
		GRAPHNAME.begin(), ::toupper);

	std::size_t cl0size = CL0Context.size();

	// Print file
	//========================================================
	CL0File<<
	"/******************************************************\n"
	" * This file is generated by RPUCompiler automatically.\n"
	" * ----------------------------------------------------\n"
	" */\n"
	"\n\n"
	"#ifndef "<<GRAPHNAME<<"_CL0_H\n"
	"#define "<<GRAPHNAME<<"_CL0_H\n"
	"\n"
	"\n"
	"#define "<<GRAPHNAME<<"_LOAD_BASE\t\t"<<SSRAM_LOAD_BASE<<"\n"
	"#define "<<GRAPHNAME<<"_STORE_BASE\t\t"<<SSRAM_STORE_BASE<<"\n"
	"\n\n"
	"#define "<<GRAPHNAME<<"_CL0_SIZE\t"<<std::dec<<cl0size<<"\n\n"
	"unsigned int "<<graphName<<"_cl0"<<"_ctx [] = {";

	CL0File.fill('0');

	for(std::size_t i =0; i <cl0size ; ++i){

		CL0File<<"\n\t\t0x"<<std::setw(8)<<std::hex<<CL0Context[i];

			if( i + 1 != cl0size)CL0File<<",";
	}

	CL0File<<"\n};\n\n#endif"<<std::endl;

	return 0;
}

#endif

#if 0

int RPUConfig::createCL0File(const String &fileName){

	std::ofstream CL0File(fileName.c_str());

	assert(CL0File);

	const String & graphName = dfgGraph.name();

	// uppercase of name
	String GRAPHNAME = graphName;
	std::transform(GRAPHNAME.begin(), GRAPHNAME.end(), 
		GRAPHNAME.begin(), ::toupper);

	std::size_t cl0size = CL0Context.size();

	// Print file
	//========================================================

	CL0File<<"#ifndef MAIN_C\n"
			"#define MAIN_C\n\n"<<std::endl;

	CL0File<<
	"/*************************************************************\n"
	" * This main.c file is generated by RPUCompiler automatically.\n"
	" * -----------------------------------------------------------\n"
	"*/\n\n"
	"// General Purpose Data Types \n"
	"typedef char            S8;  /* signed   8 -bit integer */ \n"
	"typedef short           S16; /* signed   16-bit integer */ \n"
	"typedef long            S32; /* signed   32-bit integer */ \n"
	"typedef unsigned char   U8;  /* unsigned 8 -bit integer */ \n"
	"typedef unsigned short  U16; /* unsigned 16-bit integer */ \n"
	"typedef unsigned long   U32; /* unsigned 32-bit integer */ \n\n"

	"typedef volatile U32 *  RP; \n"
	"typedef volatile U16 *  RP16; \n"
	"typedef volatile U8  *  RP8; \n"

	"\n\n// Hardware REG\n"
	"#define  RCA_cwi              0xc0000000\n"
	"#define  RCA_cwi_left_space   0xc0000004\n"
	"#define  RCA_int_clear        0xc0000030\n"
	"#define  RCA_cwi_release      0xc0000040\n\n"
	"#define  RCA_cwi2             0xc0001000\n"
	"#define  RCA_cwi_left_space2  0xc0001004\n"
	"#define  RCA_int_clear2       0xc0001030\n\n"
	"#define  ahb2fb_base          0xd0000000\n\n"



	"void main( )\n"
	"{";	

	CL0File.fill('0');

	//std::size_t j=0;

	reg32 cfghead;

	cfghead =CL0Context[0];

	int REDLREDSValue;
	REDLREDSValue = ((cfghead>>2)&0x1f);
	REDLREDSValue += ((cfghead>>7)&0x1f);

	unsigned int rcaCounter;
	rcaCounter=0;

	CL0File<<"\n\n  //this is for RCA 0"<<"\n\n";
	rcaCounter++;

	int cfgheadaddr;
	cfgheadaddr=0;

	for(std::size_t i =0; i <cl0size ; ++i)
	{

		if(i<= (1+cfgheadaddr+(REDLREDSValue *2)))
		{
	
			
			CL0File<<"  *(RP)(RCA_cwi)= 0x"<<std::setw(8)<<std::hex<<CL0Context[i]<<";\n";
		}

		if(i ==(cfgheadaddr+(REDLREDSValue*2)+2))
		{
			int temp_i;
			temp_i=i;

			cfghead =CL0Context[i];

			REDLREDSValue = ((cfghead>>2)&0x1f);
			REDLREDSValue += ((cfghead>>7)&0x1f);

			cfgheadaddr = i;

			i=temp_i - 1;

			CL0File<<"\n\n  //this is for RCA "<<std::dec<<rcaCounter<<"\n\n";
			rcaCounter++;//update this var value
		
		}
	}

	CL0File<<"\n\n  while(1);\n";

	CL0File<<"\n}\n\n#endif\n\n"<<std::endl;

	return 0;
}


#endif

/*int RPUConfig::createCL0File(const String &fileName){

	std::ofstream CL0File(fileName.c_str());

	assert(CL0File);

	//const String & graphName = dfgGraph.name();

	// uppercase of name
	//String GRAPHNAME = graphName;
	//std::transform(GRAPHNAME.begin(), GRAPHNAME.end(), 
	//	GRAPHNAME.begin(), ::toupper);

	std::size_t cl0size = CL0Context.size();

	// Print file
	//========================================================

	CL0File<<"[CWIPACKET]\n";

	CL0File<<"Config Word Cnt="<<cl0size<<std::endl;

	
	CL0File.fill('0');


	for(std::size_t i =0; i <cl0size ; ++i)
		CL0File<<"ConfigWord["<<std::dec<<i<<"]=0x"<<std::setw(8)<<std::hex<<CL0Context[i]<<";\n";

	CL0File<<"\n\n"<<std::endl;

	return 0;
}
*/

// 2012.7.21 TangSZ tmp
// C_CL0 and GRPLink generated with each DFG (one group)
// CL0 generated once
// configTotal hasn't been initilized except Context
// RPUGroupNumber, onRCANumber incorrect values

//int RPUConfig::createCL0File(const String &fileName, const String &C_fileName){
//
//	std::ofstream CL0File(fileName.c_str());
//	std::ofstream C_CL0File(C_fileName.c_str());
//
//	assert(CL0File);
//	assert(C_CL0File);
	
// 2012.7.21 TangSZ
//
//int RPUConfig::createCL0File(std::ofstream &CL0FileRef, std::ofstream &GRPLinkRef){
//	// CL0 Context Generator
//	for(Vector<Vector<reg32> >::iterator CL0GrpIter = CL0Context.begin(); CL0GrpIter != CL0Context.end(); ++CL0GrpIter)
//	{	
//		Vector<reg32> curCtx = * CL0GrpIter;
//		CL0FileRef<<"#define GROUP"<<RPUGroupNumber()<<"_"<<onRCANumber()<<"_"<<(CL0GrpIter-CL0Context.begin())<<"_CWI\\\n";
//		char buf[30];
//		sprintf(buf,"GROUP%d_%d_%d_CWI",RPUGroupNumber(),onRCANumber(),CL0GrpIter-CL0Context.begin());
//		GRPLinkRef<<"\t\t\t"<<buf<<"\\"<<std::endl;
//		GRPLinkRef<<"\t\t\twhile(!RPU0_done){}\\"<<std::endl;
//		GRPLinkRef<<"\t\t\tRPU0_done = 0;\\"<<std::endl;
//		CL0FileRef.fill('0');
//		for(Vector<reg32>::iterator CtxIter = curCtx.begin(); CtxIter != curCtx.end(); ++CtxIter)
//		{
//			CL0FileRef<<"\t\t\twrite_reg(AHB1_S1_RPU0,0x"<<std::setw(8)<<std::hex<<(*CtxIter)<<");\\\n";
//		}
//					
//		CL0FileRef<<"\t\t\twrite_reg(AHB1_S1_RPU0,0x0000000"<<std::setw(1)<<std::hex<<onRCANumber()<<");\\\n";
//		CL0FileRef<<"\t\t\twrite_reg(AHB1_S1_RPU0,0x00008000);\n\n\n\n\n";
//	}
//	return 0;
//}
//
//
//int RPUConfig::createC_CL0File(std::ofstream &C_CL0FileRef){		
//	std::size_t cl0cnt = 0;
//			
//	for(Vector<Vector<reg32> >::iterator C_CtxGrpIter = C_CL0Context.begin(); C_CtxGrpIter != C_CL0Context.end(); ++ C_CtxGrpIter)
//	{
//		Vector<reg32> C_CtxGrp = * C_CtxGrpIter;
//		cl0cnt += C_CtxGrp.size();
//	}
//
//	C_CL0FileRef<<"[CWIPACKET]\n"<<"Config Word Cnt="<<std::dec<<cl0cnt<<"\n";
//
//	C_CL0FileRef.fill('0');
//
//	std::size_t ctxCnt = 0;
//	for(Vector<Vector<reg32> >::iterator C_CtxGrpIter = C_CL0Context.begin(); C_CtxGrpIter != C_CL0Context.end(); ++ C_CtxGrpIter)
//	{
//		Vector<reg32> C_CtxGrp = * C_CtxGrpIter;
//		for(Vector<reg32>::iterator C_CtxIter = C_CtxGrp.begin(); C_CtxIter != C_CtxGrp.end(); ++C_CtxIter)
//		{
//			C_CL0FileRef<<"ConfigWord["<<std::dec<<ctxCnt<<"]=0x"<<std::setw(8)<<std::hex<<(*C_CtxIter)<<";\n";
//			ctxCnt++;
//		}
//	}
//
//	C_CL0FileRef<<"\n\n\n\n";
//	C_CL0FileRef.close();
//
//	return 0;
//}

